package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwPartnerWbAccount;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.finance.TcwPartnerWbAccountMapper;
import com.ys.service.finance.TcwPartnerWbAccountService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("TcwPartnerWbAccountService")
public class TcwPartnerWbAccountImpl implements TcwPartnerWbAccountService {

    @Autowired
    private TcwPartnerWbAccountMapper tcwPartnerWbAccountMapper;


    @Autowired
    private CheckUtil checkUtil;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwPartnerWbAccount tcwPartnerWbAccount = JSON.parseObject(JSON.toJSONString(params), TcwPartnerWbAccount.class);
        if (new TcwPartnerWbAccount().setName(tcwPartnerWbAccount.getName()).count() != 0){
            return R.error("合作方名称重复,核实后再操作!");
        }
        String loginUserId = ShiroUtils.getUserId();
        TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
        Long createChangeId = tsUserChange.getId();
        tcwPartnerWbAccount.setCreateChangeId(String.valueOf(createChangeId));
        tcwPartnerWbAccountMapper.insert(tcwPartnerWbAccount);
        return R.ok(AddDto.gotoEdit("/web/finance/tcwPartnerWbAccount/toEdit?id=" + tcwPartnerWbAccount.getId()));
    }

    /**
     * 修改币种
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

//        验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwPartnerWbAccount tcwPartnerWbAccount = JSON.parseObject(JSON.toJSONString(params), TcwPartnerWbAccount.class);
        if (new TcwPartnerWbAccount().setName(tcwPartnerWbAccount.getName()).where("A.id != " + tcwPartnerWbAccount.getId()).count() != 0){
            return R.error("合作方名称重复,核实后再操作!");
        }
        tcwPartnerWbAccountMapper.update(tcwPartnerWbAccount);
        return R.ok(AddDto.gotoEdit("/web/finance/tcwPartnerWbAccount/toEdit?id=" + tcwPartnerWbAccount.getId()));
    }


    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+ MenuId.TCW_PARTNERWBACCOUNT)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TCW_PartnerWbAccount",
                    listField,
                    infoMap,
                    ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
